package com.controller;

import com.domain.dto.ChatDto;
import com.domain.entity.ChatMessage;
import com.domain.entity.User;
import com.service.ChatMessageService;
import com.service.UserFriendService;
import com.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/chat")
public class ChatController {
    @Autowired
    ChatMessageService chatMessageService;
    @Autowired
    UsersService usersService;
    @Autowired
    UserFriendService userFriendService;

    /**
     * 根据时间进行搜索历史消息
     *
     * @param minTime
     * @param maxTime
     * @param request
     * @param receiveId
     * @param type
     * @return
     */
    @GetMapping("/history")
    @Transactional
    public List<ChatDto> getHistoryChat(long minTime, long maxTime, HttpServletRequest request, long receiveId, Integer type) {
        User loginUser = usersService.getLoginUser(request);
        long currentTimeMillis = System.currentTimeMillis();
        List<ChatMessage> messages =chatMessageService.getChatMessages(minTime, maxTime, receiveId, type, loginUser,currentTimeMillis);
        return messages.stream().map(chatMessage -> {
            ChatDto chatDto = new ChatDto();
            chatDto.setChatMessage(chatMessage);
            return chatDto;
        }).collect(Collectors.toList());
    }




}
